package com.haoma.service.impl;

import com.haoma.elasticsearch.ArticleEs;
import com.haoma.elasticsearch.ArticleMapper;
import com.haoma.entity.Message;
import com.haoma.entity.TalkUser;
import com.haoma.entity.Visitor;
import com.haoma.mapper.TalkUserMapper;
import com.haoma.service.HomeService;
import com.haoma.service.VisitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Description:
 */
@Service
public class HomeServiceImpl implements HomeService {

    @Autowired
    private TalkServiceImpl talkService;

    @Autowired
    private MessageServiceImpl messageService;

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private CategoryServiceImpl CategoryServiceImpl;

    @Autowired
    private LabelServiceImpl labelService;

    @Autowired
    private TalkUserMapper talkUserMapper;

    @Autowired
    private PhotoServiceImpl photoService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private VisitorService visitorService;

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 统计所有的数量
     *
     * @return
     */
    @Transactional
    public Map<String, Long> getCount() {
        // 统计说说数量
        long talkCount = talkService.count();

        // 统计留言数量
        List<Message> all = mongoTemplate.findAll(Message.class);
        // 统计文章数量
        long articleCount = articleMapper.count();

        // 统计分类数量
        long categoryCount = CategoryServiceImpl.count();

        // 统计标签数量
        long labelCount = labelService.count();

        // 统计用户数量
        List<TalkUser> talkUserList = talkUserMapper.selectList(null);
        long userCount = talkUserList.size() + talkCount;

        // 统计相册数量
        long photoCount = photoService.count();

        // 统计网站访问量
        Object requestCount = redisTemplate.opsForValue().get("request-count");
        Long aLong = Long.valueOf(requestCount.toString());
        Map<String, Long> map = new HashMap();
        map.put("talkCount", talkCount);
        map.put("messageCount", Long.valueOf(all.size()));
        map.put("articleCount", articleCount);
        map.put("categoryCount", categoryCount);
        map.put("labelCount", labelCount);
        map.put("userCount", userCount);
        map.put("photoCount", photoCount);
        map.put("requestCount", aLong);
        return map;
    }

    /**
     * 获取热度前8的文章
     *
     * @return
     */
    public Map<String, List<Object>> getHotTop8() {
        PageRequest of = PageRequest.of(0, 7);
        Page<ArticleEs> articleEs = articleMapper.findByOrderByHotDesc(of);
        // 返回图标需要的数据
        // 名称
        List<String> titleList = articleEs.stream().map(ArticleEs::getTitle).toList();
        // 热度
        List<Integer> hotList = articleEs.stream().map(ArticleEs::getHot).toList();
        Map<String, List<Object>> listMap = new HashMap<>();
        listMap.put("titleList", Collections.singletonList(titleList));
        listMap.put("hotList", Collections.singletonList(hotList));
        return listMap;
    }

    /**
     * 获取到近7天的访客数量
     *
     * @return
     */
    public Map<String, List<Object>> getVisitor() {
        // 获取到近7日的访客数量
        List<Visitor> visitorList = visitorService.getVisitorList();
        Collections.reverse(visitorList);
        // 提取日期
        List<String> timeList = visitorList.stream().map(item -> {
            String time = item.getTime();
            String substring = time.substring(6, 10);
            return substring;
        }).toList();
        // 提取数量
        List<Integer> countList = visitorList.stream().map(Visitor::getCount).toList();
        Map<String, List<Object>> listMap = new HashMap<>();
        listMap.put("timeList", Collections.singletonList(timeList));
        listMap.put("countList", Collections.singletonList(countList));
        return listMap;
    }
}
